<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE CPP #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE Trustworthy #-}</span><span>
</span><span id="line-3"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-4"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-5"></span><span class="hs-comment">-- Module      : Data.Binary</span><span>
</span><span id="line-6"></span><span class="hs-comment">-- Copyright   : Lennart Kolmodin</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- License     : BSD3-style (see LICENSE)</span><span>
</span><span id="line-8"></span><span class="hs-comment">--</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- Maintainer  : Lennart Kolmodin &lt;kolmodin@gmail.com&gt;</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- Stability   : unstable</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Portability : portable to Hugs and GHC. Requires the FFI and some flexible instances.</span><span>
</span><span id="line-12"></span><span class="hs-comment">--</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Binary serialisation of Haskell values to and from lazy 'ByteString's.</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- The Binary library provides methods for encoding Haskell values as</span><span>
</span><span id="line-15"></span><span class="hs-comment">-- streams of bytes directly in memory. The resulting 'ByteString' can</span><span>
</span><span id="line-16"></span><span class="hs-comment">-- then be written to disk, sent over the network, or further processed</span><span>
</span><span id="line-17"></span><span class="hs-comment">-- (for example, compressed with gzip).</span><span>
</span><span id="line-18"></span><span class="hs-comment">--</span><span>
</span><span id="line-19"></span><span class="hs-comment">-- The @binary@ package is notable in that it provides both pure, and</span><span>
</span><span id="line-20"></span><span class="hs-comment">-- high performance serialisation.</span><span>
</span><span id="line-21"></span><span class="hs-comment">--</span><span>
</span><span id="line-22"></span><span class="hs-comment">-- Values encoded using the 'Binary' class are always encoded in network order</span><span>
</span><span id="line-23"></span><span class="hs-comment">-- (big endian) form, and encoded data should be portable across</span><span>
</span><span id="line-24"></span><span class="hs-comment">-- machine endianness, word size, or compiler version. For example,</span><span>
</span><span id="line-25"></span><span class="hs-comment">-- data encoded using the 'Binary' class could be written on any machine,</span><span>
</span><span id="line-26"></span><span class="hs-comment">-- and read back on any another.</span><span>
</span><span id="line-27"></span><span class="hs-comment">--</span><span>
</span><span id="line-28"></span><span class="hs-comment">-- If the specifics of the data format is not important to you, for example,</span><span>
</span><span id="line-29"></span><span class="hs-comment">-- you are more interested in serializing and deserializing values than</span><span>
</span><span id="line-30"></span><span class="hs-comment">-- in which format will be used, it is possible to derive 'Binary'</span><span>
</span><span id="line-31"></span><span class="hs-comment">-- instances using the generic support. See 'GBinaryGet' and</span><span>
</span><span id="line-32"></span><span class="hs-comment">-- 'GBinaryPut'.</span><span>
</span><span id="line-33"></span><span class="hs-comment">--</span><span>
</span><span id="line-34"></span><span class="hs-comment">-- If you have specific requirements about the encoding format, you can use</span><span>
</span><span id="line-35"></span><span class="hs-comment">-- the encoding and decoding primitives directly, see the modules</span><span>
</span><span id="line-36"></span><span class="hs-comment">-- &quot;Data.Binary.Get&quot; and &quot;Data.Binary.Put&quot;.</span><span>
</span><span id="line-37"></span><span class="hs-comment">--</span><span>
</span><span id="line-38"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-39"></span><span>
</span><span id="line-40"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">Data.Binary</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-41"></span><span>
</span><span id="line-42"></span><span>    </span><span class="annot"><span class="hs-comment">-- * The Binary class</span></span><span>
</span><span id="line-43"></span><span>      </span><span class="annot"><a href="Data.Binary.Class.html#Binary"><span class="hs-identifier">Binary</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span>
</span><span id="line-44"></span><span>    </span><span class="annot"><span class="hs-comment">-- ** Example</span></span><span>
</span><span id="line-45"></span><span>    </span><span class="annot"><span class="hs-comment">-- $example</span></span><span>
</span><span id="line-46"></span><span>
</span><span id="line-47"></span><span>    </span><span class="annot"><span class="hs-comment">-- * Generic support</span></span><span>
</span><span id="line-48"></span><span>    </span><span class="annot"><span class="hs-comment">-- $generics</span></span><span>
</span><span id="line-49"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.Class.html#GBinaryGet"><span class="hs-identifier">GBinaryGet</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span>
</span><span id="line-50"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.Class.html#GBinaryPut"><span class="hs-identifier">GBinaryPut</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span>
</span><span id="line-51"></span><span>
</span><span id="line-52"></span><span>    </span><span class="annot"><span class="hs-comment">-- * The Get and Put monads</span></span><span>
</span><span id="line-53"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.Get.Internal.html#Get"><span class="hs-identifier">Get</span></a></span><span>
</span><span id="line-54"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.Put.html#Put"><span class="hs-identifier">Put</span></a></span><span>
</span><span id="line-55"></span><span>
</span><span id="line-56"></span><span>    </span><span class="annot"><span class="hs-comment">-- * Useful helpers for writing instances</span></span><span>
</span><span id="line-57"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.Put.html#putWord8"><span class="hs-identifier">putWord8</span></a></span><span>
</span><span id="line-58"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.Get.html#getWord8"><span class="hs-identifier">getWord8</span></a></span><span>
</span><span id="line-59"></span><span>
</span><span id="line-60"></span><span>    </span><span class="annot"><span class="hs-comment">-- * Binary serialisation</span></span><span>
</span><span id="line-61"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.html#encode"><span class="hs-identifier">encode</span></a></span><span>                    </span><span class="hs-comment">-- :: Binary a =&gt; a -&gt; ByteString</span><span>
</span><span id="line-62"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.html#decode"><span class="hs-identifier">decode</span></a></span><span>                    </span><span class="hs-comment">-- :: Binary a =&gt; ByteString -&gt; a</span><span>
</span><span id="line-63"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.html#decodeOrFail"><span class="hs-identifier">decodeOrFail</span></a></span><span>
</span><span id="line-64"></span><span>
</span><span id="line-65"></span><span>    </span><span class="annot"><span class="hs-comment">-- * IO functions for serialisation</span></span><span>
</span><span id="line-66"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.html#encodeFile"><span class="hs-identifier">encodeFile</span></a></span><span>                </span><span class="hs-comment">-- :: Binary a =&gt; FilePath -&gt; a -&gt; IO ()</span><span>
</span><span id="line-67"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.html#decodeFile"><span class="hs-identifier">decodeFile</span></a></span><span>                </span><span class="hs-comment">-- :: Binary a =&gt; FilePath -&gt; IO a</span><span>
</span><span id="line-68"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.html#decodeFileOrFail"><span class="hs-identifier">decodeFileOrFail</span></a></span><span>
</span><span id="line-69"></span><span>
</span><span id="line-70"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="hs-keyword">module</span><span> </span><span class="annot"><a href="../../base/src/Data.Word.html#"><span class="hs-identifier">Data.Word</span></a></span><span> </span><span class="hs-comment">-- useful</span><span>
</span><span id="line-71"></span><span>
</span><span id="line-72"></span><span>    </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-73"></span><span>
</span><span id="line-74"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Data.Word.html#"><span class="hs-identifier">Data.Word</span></a></span><span>
</span><span id="line-75"></span><span>
</span><span id="line-76"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Binary.Class.html"><span class="hs-identifier">Data.Binary.Class</span></a></span><span>
</span><span id="line-77"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Binary.Put.html"><span class="hs-identifier">Data.Binary.Put</span></a></span><span>
</span><span id="line-78"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Binary.Get.html"><span class="hs-identifier">Data.Binary.Get</span></a></span><span>
</span><span id="line-79"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Binary.Generic.html"><span class="hs-identifier">Data.Binary.Generic</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-80"></span><span>
</span><span id="line-81"></span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">qualified</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.html#"><span class="hs-identifier">Data.ByteString</span></a></span><span> </span><span class="hs-keyword">as</span><span> </span><span class="annot"><span class="hs-identifier">B</span></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.html#hGet"><span class="hs-identifier">hGet</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.html#length"><span class="hs-identifier">length</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-82"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.html#"><span class="hs-identifier">Data.ByteString.Lazy</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.Internal.html#ByteString"><span class="hs-identifier">ByteString</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-83"></span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">qualified</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.html#"><span class="hs-identifier">Data.ByteString.Lazy</span></a></span><span> </span><span class="hs-keyword">as</span><span> </span><span class="annot"><span class="hs-identifier">L</span></span><span>
</span><span id="line-84"></span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">qualified</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.Internal.html#"><span class="hs-identifier">Data.ByteString.Lazy.Internal</span></a></span><span> </span><span class="hs-keyword">as</span><span> </span><span class="annot"><span class="hs-identifier">L</span></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.Internal.html#defaultChunkSize"><span class="hs-identifier">defaultChunkSize</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-85"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/System.IO.html#"><span class="hs-identifier">System.IO</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="../../base/src/System.IO.html#withBinaryFile"><span class="hs-identifier">withBinaryFile</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.IOMode.html#IOMode"><span class="hs-identifier">IOMode</span></a></span><span class="hs-special">(</span><span class="annot"><a href="../../base/src/GHC.IO.IOMode.html#ReadMode"><span class="hs-identifier">ReadMode</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-86"></span><span>
</span><span id="line-87"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-88"></span><span>
</span><span id="line-89"></span><span class="hs-comment">-- $example</span><span>
</span><span id="line-90"></span><span class="hs-comment">-- To serialise a custom type, an instance of Binary for that type is</span><span>
</span><span id="line-91"></span><span class="hs-comment">-- required. For example, suppose we have a data structure:</span><span>
</span><span id="line-92"></span><span class="hs-comment">--</span><span>
</span><span id="line-93"></span><span class="hs-comment">-- &gt; data Exp = IntE Int</span><span>
</span><span id="line-94"></span><span class="hs-comment">-- &gt;          | OpE  String Exp Exp</span><span>
</span><span id="line-95"></span><span class="hs-comment">-- &gt;    deriving Show</span><span>
</span><span id="line-96"></span><span class="hs-comment">--</span><span>
</span><span id="line-97"></span><span class="hs-comment">-- We can encode values of this type into bytestrings using the</span><span>
</span><span id="line-98"></span><span class="hs-comment">-- following instance, which proceeds by recursively breaking down the</span><span>
</span><span id="line-99"></span><span class="hs-comment">-- structure to serialise:</span><span>
</span><span id="line-100"></span><span class="hs-comment">--</span><span>
</span><span id="line-101"></span><span class="hs-comment">-- &gt; instance Binary Exp where</span><span>
</span><span id="line-102"></span><span class="hs-comment">-- &gt;       put (IntE i)      = do put (0 :: Word8)</span><span>
</span><span id="line-103"></span><span class="hs-comment">-- &gt;                              put i</span><span>
</span><span id="line-104"></span><span class="hs-comment">-- &gt;       put (OpE s e1 e2) = do put (1 :: Word8)</span><span>
</span><span id="line-105"></span><span class="hs-comment">-- &gt;                              put s</span><span>
</span><span id="line-106"></span><span class="hs-comment">-- &gt;                              put e1</span><span>
</span><span id="line-107"></span><span class="hs-comment">-- &gt;                              put e2</span><span>
</span><span id="line-108"></span><span class="hs-comment">-- &gt;</span><span>
</span><span id="line-109"></span><span class="hs-comment">-- &gt;       get = do t &lt;- get :: Get Word8</span><span>
</span><span id="line-110"></span><span class="hs-comment">-- &gt;                case t of</span><span>
</span><span id="line-111"></span><span class="hs-comment">-- &gt;                     0 -&gt; do i &lt;- get</span><span>
</span><span id="line-112"></span><span class="hs-comment">-- &gt;                             return (IntE i)</span><span>
</span><span id="line-113"></span><span class="hs-comment">-- &gt;                     1 -&gt; do s  &lt;- get</span><span>
</span><span id="line-114"></span><span class="hs-comment">-- &gt;                             e1 &lt;- get</span><span>
</span><span id="line-115"></span><span class="hs-comment">-- &gt;                             e2 &lt;- get</span><span>
</span><span id="line-116"></span><span class="hs-comment">-- &gt;                             return (OpE s e1 e2)</span><span>
</span><span id="line-117"></span><span class="hs-comment">--</span><span>
</span><span id="line-118"></span><span class="hs-comment">-- Note how we write an initial tag byte to indicate each variant of the</span><span>
</span><span id="line-119"></span><span class="hs-comment">-- data type.</span><span>
</span><span id="line-120"></span><span class="hs-comment">--</span><span>
</span><span id="line-121"></span><span class="hs-comment">-- We can simplify the writing of 'get' instances using monadic</span><span>
</span><span id="line-122"></span><span class="hs-comment">-- combinators:</span><span>
</span><span id="line-123"></span><span class="hs-comment">--</span><span>
</span><span id="line-124"></span><span class="hs-comment">-- &gt;       get = do tag &lt;- getWord8</span><span>
</span><span id="line-125"></span><span class="hs-comment">-- &gt;                case tag of</span><span>
</span><span id="line-126"></span><span class="hs-comment">-- &gt;                    0 -&gt; liftM  IntE get</span><span>
</span><span id="line-127"></span><span class="hs-comment">-- &gt;                    1 -&gt; liftM3 OpE  get get get</span><span>
</span><span id="line-128"></span><span class="hs-comment">--</span><span>
</span><span id="line-129"></span><span class="hs-comment">-- To serialise this to a bytestring, we use 'encode', which packs the</span><span>
</span><span id="line-130"></span><span class="hs-comment">-- data structure into a binary format, in a lazy bytestring</span><span>
</span><span id="line-131"></span><span class="hs-comment">--</span><span>
</span><span id="line-132"></span><span class="hs-comment">-- &gt; &gt; let e = OpE &quot;*&quot; (IntE 7) (OpE &quot;/&quot; (IntE 4) (IntE 2))</span><span>
</span><span id="line-133"></span><span class="hs-comment">-- &gt; &gt; let v = encode e</span><span>
</span><span id="line-134"></span><span class="hs-comment">--</span><span>
</span><span id="line-135"></span><span class="hs-comment">-- Where @v@ is a binary encoded data structure. To reconstruct the</span><span>
</span><span id="line-136"></span><span class="hs-comment">-- original data, we use 'decode'</span><span>
</span><span id="line-137"></span><span class="hs-comment">--</span><span>
</span><span id="line-138"></span><span class="hs-comment">-- &gt; &gt; decode v :: Exp</span><span>
</span><span id="line-139"></span><span class="hs-comment">-- &gt; OpE &quot;*&quot; (IntE 7) (OpE &quot;/&quot; (IntE 4) (IntE 2))</span><span>
</span><span id="line-140"></span><span class="hs-comment">--</span><span>
</span><span id="line-141"></span><span class="hs-comment">-- The lazy ByteString that results from 'encode' can be written to</span><span>
</span><span id="line-142"></span><span class="hs-comment">-- disk, and read from disk using Data.ByteString.Lazy IO functions,</span><span>
</span><span id="line-143"></span><span class="hs-comment">-- such as hPutStr or writeFile:</span><span>
</span><span id="line-144"></span><span class="hs-comment">--</span><span>
</span><span id="line-145"></span><span class="hs-comment">-- &gt; &gt; writeFile &quot;/tmp/exp.txt&quot; (encode e)</span><span>
</span><span id="line-146"></span><span class="hs-comment">--</span><span>
</span><span id="line-147"></span><span class="hs-comment">-- And read back with:</span><span>
</span><span id="line-148"></span><span class="hs-comment">--</span><span>
</span><span id="line-149"></span><span class="hs-comment">-- &gt; &gt; readFile &quot;/tmp/exp.txt&quot; &gt;&gt;= return . decode :: IO Exp</span><span>
</span><span id="line-150"></span><span class="hs-comment">-- &gt; OpE &quot;*&quot; (IntE 7) (OpE &quot;/&quot; (IntE 4) (IntE 2))</span><span>
</span><span id="line-151"></span><span class="hs-comment">--</span><span>
</span><span id="line-152"></span><span class="hs-comment">-- We can also directly serialise a value to and from a Handle, or a file:</span><span>
</span><span id="line-153"></span><span class="hs-comment">--</span><span>
</span><span id="line-154"></span><span class="hs-comment">-- &gt; &gt; v &lt;- decodeFile  &quot;/tmp/exp.txt&quot; :: IO Exp</span><span>
</span><span id="line-155"></span><span class="hs-comment">-- &gt; OpE &quot;*&quot; (IntE 7) (OpE &quot;/&quot; (IntE 4) (IntE 2))</span><span>
</span><span id="line-156"></span><span class="hs-comment">--</span><span>
</span><span id="line-157"></span><span class="hs-comment">-- And write a value to disk</span><span>
</span><span id="line-158"></span><span class="hs-comment">--</span><span>
</span><span id="line-159"></span><span class="hs-comment">-- &gt; &gt; encodeFile &quot;/tmp/a.txt&quot; v</span><span>
</span><span id="line-160"></span><span class="hs-comment">--</span><span>
</span><span id="line-161"></span><span>
</span><span id="line-162"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-163"></span><span class="hs-comment">-- Wrappers to run the underlying monad</span><span>
</span><span id="line-164"></span><span>
</span><span id="line-165"></span><span class="hs-comment">-- | Encode a value using binary serialisation to a lazy ByteString.</span><span>
</span><span id="line-166"></span><span class="hs-comment">--</span><span>
</span><span id="line-167"></span><span id="local-6989586621679059268"><span class="annot"><a href="Data.Binary.html#encode"><span class="hs-identifier hs-type">encode</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Data.Binary.Class.html#Binary"><span class="hs-identifier hs-type">Binary</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679059268"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679059268"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.Internal.html#ByteString"><span class="hs-identifier hs-type">ByteString</span></a></span></span><span>
</span><span id="line-168"></span><span id="encode"><span class="annot"><span class="annottext">encode :: forall a. Binary a =&gt; a -&gt; ByteString
</span><a href="Data.Binary.html#encode"><span class="hs-identifier hs-var hs-var">encode</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Put -&gt; ByteString
</span><a href="Data.Binary.Put.html#runPut"><span class="hs-identifier hs-var">runPut</span></a></span><span> </span><span class="annot"><span class="annottext">(Put -&gt; ByteString) -&gt; (a -&gt; Put) -&gt; a -&gt; ByteString
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="../../base/src/GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; Put
forall t. Binary t =&gt; t -&gt; Put
</span><a href="Data.Binary.Class.html#put"><span class="hs-identifier hs-var">put</span></a></span><span>
</span><span id="line-169"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="annot"><a href="Data.Binary.html#encode"><span class="hs-pragma hs-type">encode</span></a></span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-170"></span><span>
</span><span id="line-171"></span><span class="hs-comment">-- | Decode a value from a lazy ByteString, reconstructing the original structure.</span><span>
</span><span id="line-172"></span><span id="local-6989586621679059259"><span class="annot"><a href="Data.Binary.html#decode"><span class="hs-identifier hs-type">decode</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Data.Binary.Class.html#Binary"><span class="hs-identifier hs-type">Binary</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679059259"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.Internal.html#ByteString"><span class="hs-identifier hs-type">ByteString</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679059259"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-173"></span><span id="decode"><span class="annot"><span class="annottext">decode :: forall a. Binary a =&gt; ByteString -&gt; a
</span><a href="Data.Binary.html#decode"><span class="hs-identifier hs-var hs-var">decode</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Get a -&gt; ByteString -&gt; a
forall a. Get a -&gt; ByteString -&gt; a
</span><a href="Data.Binary.Get.html#runGet"><span class="hs-identifier hs-var">runGet</span></a></span><span> </span><span class="annot"><span class="annottext">Get a
forall t. Binary t =&gt; Get t
</span><a href="Data.Binary.Class.html#get"><span class="hs-identifier hs-var">get</span></a></span><span>
</span><span id="line-174"></span><span>
</span><span id="line-175"></span><span class="hs-comment">-- | Decode a value from a lazy ByteString. Returning 'Left' on failure and</span><span>
</span><span id="line-176"></span><span class="hs-comment">-- 'Right' on success. In both cases the unconsumed input and the number of</span><span>
</span><span id="line-177"></span><span class="hs-comment">-- consumed bytes is returned. In case of failure, a human-readable error</span><span>
</span><span id="line-178"></span><span class="hs-comment">-- message will be returned as well.</span><span>
</span><span id="line-179"></span><span class="hs-comment">--</span><span>
</span><span id="line-180"></span><span class="hs-comment">-- @since 0.7.0.0</span><span>
</span><span id="line-181"></span><span id="local-6989586621679059255"><span class="annot"><a href="Data.Binary.html#decodeOrFail"><span class="hs-identifier hs-type">decodeOrFail</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Data.Binary.Class.html#Binary"><span class="hs-identifier hs-type">Binary</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679059255"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.Internal.html#ByteString"><span class="hs-identifier hs-type">L.ByteString</span></a></span><span>
</span><span id="line-182"></span><span>             </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../base/src/Data.Either.html#Either"><span class="hs-identifier hs-type">Either</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.Internal.html#ByteString"><span class="hs-identifier hs-type">L.ByteString</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.Get.html#ByteOffset"><span class="hs-identifier hs-type">ByteOffset</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-183"></span><span>                       </span><span class="hs-special">(</span><span class="annot"><a href="../../bytestring/src/Data.ByteString.Lazy.Internal.html#ByteString"><span class="hs-identifier hs-type">L.ByteString</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Binary.Get.html#ByteOffset"><span class="hs-identifier hs-type">ByteOffset</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="#local-6989586621679059255"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-184"></span><span id="decodeOrFail"><span class="annot"><span class="annottext">decodeOrFail :: forall a.
Binary a =&gt;
ByteString
-&gt; Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
</span><a href="Data.Binary.html#decodeOrFail"><span class="hs-identifier hs-var hs-var">decodeOrFail</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Get a
-&gt; ByteString
-&gt; Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a.
Get a
-&gt; ByteString
-&gt; Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
</span><a href="Data.Binary.Get.html#runGetOrFail"><span class="hs-identifier hs-var">runGetOrFail</span></a></span><span> </span><span class="annot"><span class="annottext">Get a
forall t. Binary t =&gt; Get t
</span><a href="Data.Binary.Class.html#get"><span class="hs-identifier hs-var">get</span></a></span><span>
</span><span id="line-185"></span><span>
</span><span id="line-186"></span><span>
</span><span id="line-187"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-188"></span><span class="hs-comment">-- Convenience IO operations</span><span>
</span><span id="line-189"></span><span>
</span><span id="line-190"></span><span class="hs-comment">-- | Lazily serialise a value to a file.</span><span>
</span><span id="line-191"></span><span class="hs-comment">--</span><span>
</span><span id="line-192"></span><span class="hs-comment">-- This is just a convenience function, it's defined simply as:</span><span>
</span><span id="line-193"></span><span class="hs-comment">--</span><span>
</span><span id="line-194"></span><span class="hs-comment">-- &gt; encodeFile f = B.writeFile f . encode</span><span>
</span><span id="line-195"></span><span class="hs-comment">--</span><span>
</span><span id="line-196"></span><span class="hs-comment">-- So for example if you wanted to compress as well, you could use:</span><span>
</span><span id="line-197"></span><span class="hs-comment">--</span><span>
</span><span id="line-198"></span><span class="hs-comment">-- &gt; B.writeFile f . compress . encode</span><span>
</span><span id="line-199"></span><span class="hs-comment">--</span><span>
</span><span id="line-200"></span><span id="local-6989586621679059251"><span class="annot"><a href="Data.Binary.html#encodeFile"><span class="hs-identifier hs-type">encodeFile</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Data.Binary.Class.html#Binary"><span class="hs-identifier hs-type">Binary</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679059251"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.html#FilePath"><span class="hs-identifier hs-type">FilePath</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679059251"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-201"></span><span id="encodeFile"><span class="annot"><span class="annottext">encodeFile :: forall a. Binary a =&gt; String -&gt; a -&gt; IO ()
</span><a href="Data.Binary.html#encodeFile"><span class="hs-identifier hs-var hs-var">encodeFile</span></a></span></span><span> </span><span id="local-6989586621679059197"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059197"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span id="local-6989586621679059196"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679059196"><span class="hs-identifier hs-var">v</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; ByteString -&gt; IO ()
</span><a href="../../bytestring/src/Data.ByteString.Lazy.html#writeFile"><span class="hs-identifier hs-var">L.writeFile</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059197"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a -&gt; ByteString
forall a. Binary a =&gt; a -&gt; ByteString
</span><a href="Data.Binary.html#encode"><span class="hs-identifier hs-var">encode</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679059196"><span class="hs-identifier hs-var">v</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-202"></span><span>
</span><span id="line-203"></span><span class="hs-comment">-- | Decode a value from a file. In case of errors, 'error' will</span><span>
</span><span id="line-204"></span><span class="hs-comment">-- be called with the error message.</span><span>
</span><span id="line-205"></span><span class="hs-comment">--</span><span>
</span><span id="line-206"></span><span class="hs-comment">-- @since 0.7.0.0</span><span>
</span><span id="line-207"></span><span id="local-6989586621679059249"><span class="annot"><a href="Data.Binary.html#decodeFile"><span class="hs-identifier hs-type">decodeFile</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Data.Binary.Class.html#Binary"><span class="hs-identifier hs-type">Binary</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679059249"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.html#FilePath"><span class="hs-identifier hs-type">FilePath</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="annot"><a href="#local-6989586621679059249"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-208"></span><span id="decodeFile"><span class="annot"><span class="annottext">decodeFile :: forall a. Binary a =&gt; String -&gt; IO a
</span><a href="Data.Binary.html#decodeFile"><span class="hs-identifier hs-var hs-var">decodeFile</span></a></span></span><span> </span><span id="local-6989586621679059187"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059187"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-209"></span><span>  </span><span id="local-6989586621679059186"><span class="annot"><span class="annottext">Either (ByteOffset, String) a
</span><a href="#local-6989586621679059186"><span class="hs-identifier hs-var">result</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO (Either (ByteOffset, String) a)
forall a. Binary a =&gt; String -&gt; IO (Either (ByteOffset, String) a)
</span><a href="Data.Binary.html#decodeFileOrFail"><span class="hs-identifier hs-var">decodeFileOrFail</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059187"><span class="hs-identifier hs-var">f</span></a></span><span>
</span><span id="line-210"></span><span>  </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Either (ByteOffset, String) a
</span><a href="#local-6989586621679059186"><span class="hs-identifier hs-var">result</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-211"></span><span>    </span><span class="annot"><a href="../../base/src/Data.Either.html#Right"><span class="hs-identifier hs-type">Right</span></a></span><span> </span><span id="local-6989586621679059185"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679059185"><span class="hs-identifier hs-var">x</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">a -&gt; IO a
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679059185"><span class="hs-identifier hs-var">x</span></a></span><span>
</span><span id="line-212"></span><span>    </span><span class="annot"><a href="../../base/src/Data.Either.html#Left"><span class="hs-identifier hs-type">Left</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ByteOffset
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span id="local-6989586621679059184"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059184"><span class="hs-identifier hs-var">str</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO a
forall a. HasCallStack =&gt; String -&gt; a
</span><a href="../../base/src/GHC.Err.html#error"><span class="hs-identifier hs-var">error</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059184"><span class="hs-identifier hs-var">str</span></a></span><span>
</span><span id="line-213"></span><span>
</span><span id="line-214"></span><span class="hs-comment">-- | Decode a value from a file. In case of success, the value will be returned</span><span>
</span><span id="line-215"></span><span class="hs-comment">-- in 'Right'. In case of decoder errors, the error message together with</span><span>
</span><span id="line-216"></span><span class="hs-comment">-- the byte offset will be returned.</span><span>
</span><span id="line-217"></span><span id="local-6989586621679059246"><span class="annot"><a href="Data.Binary.html#decodeFileOrFail"><span class="hs-identifier hs-type">decodeFileOrFail</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Data.Binary.Class.html#Binary"><span class="hs-identifier hs-type">Binary</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679059246"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.IO.html#FilePath"><span class="hs-identifier hs-type">FilePath</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="hs-identifier hs-type">IO</span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../base/src/Data.Either.html#Either"><span class="hs-identifier hs-type">Either</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Data.Binary.Get.html#ByteOffset"><span class="hs-identifier hs-type">ByteOffset</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.Base.html#String"><span class="hs-identifier hs-type">String</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><a href="#local-6989586621679059246"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-218"></span><span id="decodeFileOrFail"><span class="annot"><span class="annottext">decodeFileOrFail :: forall a. Binary a =&gt; String -&gt; IO (Either (ByteOffset, String) a)
</span><a href="Data.Binary.html#decodeFileOrFail"><span class="hs-identifier hs-var hs-var">decodeFileOrFail</span></a></span></span><span> </span><span id="local-6989586621679059180"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059180"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-219"></span><span>  </span><span class="annot"><span class="annottext">String
-&gt; IOMode
-&gt; (Handle -&gt; IO (Either (ByteOffset, String) a))
-&gt; IO (Either (ByteOffset, String) a)
forall r. String -&gt; IOMode -&gt; (Handle -&gt; IO r) -&gt; IO r
</span><a href="../../base/src/System.IO.html#withBinaryFile"><span class="hs-identifier hs-var">withBinaryFile</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059180"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">IOMode
</span><a href="../../base/src/GHC.IO.IOMode.html#ReadMode"><span class="hs-identifier hs-var">ReadMode</span></a></span><span> </span><span class="annot"><span class="annottext">((Handle -&gt; IO (Either (ByteOffset, String) a))
 -&gt; IO (Either (ByteOffset, String) a))
-&gt; (Handle -&gt; IO (Either (ByteOffset, String) a))
-&gt; IO (Either (ByteOffset, String) a)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="../../base/src/GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679059179"><span class="annot"><span class="annottext">Handle
</span><a href="#local-6989586621679059179"><span class="hs-identifier hs-var">h</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-220"></span><span>    </span><span class="annot"><span class="annottext">Decoder a -&gt; Handle -&gt; IO (Either (ByteOffset, String) a)
forall {b}.
Decoder b -&gt; Handle -&gt; IO (Either (ByteOffset, String) b)
</span><a href="#local-6989586621679059178"><span class="hs-identifier hs-var">feed</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Get a -&gt; Decoder a
forall a. Get a -&gt; Decoder a
</span><a href="Data.Binary.Get.html#runGetIncremental"><span class="hs-identifier hs-var">runGetIncremental</span></a></span><span> </span><span class="annot"><span class="annottext">Get a
forall t. Binary t =&gt; Get t
</span><a href="Data.Binary.Class.html#get"><span class="hs-identifier hs-var">get</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="#local-6989586621679059179"><span class="hs-identifier hs-var">h</span></a></span><span>
</span><span id="line-221"></span><span>  </span><span class="hs-keyword">where</span><span> </span><span class="hs-comment">-- TODO: put in Data.Binary.Get and name pushFromHandle?</span><span>
</span><span id="line-222"></span><span>    </span><span id="local-6989586621679059178"><span class="annot"><span class="annottext">feed :: Decoder b -&gt; Handle -&gt; IO (Either (ByteOffset, String) b)
</span><a href="#local-6989586621679059178"><span class="hs-identifier hs-var hs-var">feed</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Data.Binary.Get.html#Done"><span class="hs-identifier hs-type">Done</span></a></span><span> </span><span class="annot"><span class="annottext">ByteString
</span><span class="hs-identifier">_</span></span><span> </span><span class="annot"><span class="annottext">ByteOffset
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679059168"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679059168"><span class="hs-identifier hs-var">x</span></a></span></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Handle
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Either (ByteOffset, String) b -&gt; IO (Either (ByteOffset, String) b)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">b -&gt; Either (ByteOffset, String) b
forall a b. b -&gt; Either a b
</span><a href="../../base/src/Data.Either.html#Right"><span class="hs-identifier hs-var">Right</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679059168"><span class="hs-identifier hs-var">x</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-223"></span><span>    </span><span class="annot"><a href="#local-6989586621679059178"><span class="hs-identifier hs-var">feed</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Data.Binary.Get.html#Fail"><span class="hs-identifier hs-type">Fail</span></a></span><span> </span><span class="annot"><span class="annottext">ByteString
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679059166"><span class="annot"><span class="annottext">ByteOffset
</span><a href="#local-6989586621679059166"><span class="hs-identifier hs-var">pos</span></a></span></span><span> </span><span id="local-6989586621679059165"><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059165"><span class="hs-identifier hs-var">str</span></a></span></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Handle
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Either (ByteOffset, String) b -&gt; IO (Either (ByteOffset, String) b)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="../../base/src/GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">(ByteOffset, String) -&gt; Either (ByteOffset, String) b
forall a b. a -&gt; Either a b
</span><a href="../../base/src/Data.Either.html#Left"><span class="hs-identifier hs-var">Left</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ByteOffset
</span><a href="#local-6989586621679059166"><span class="hs-identifier hs-var">pos</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">String
</span><a href="#local-6989586621679059165"><span class="hs-identifier hs-var">str</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-224"></span><span>    </span><span class="annot"><a href="#local-6989586621679059178"><span class="hs-identifier hs-var">feed</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Data.Binary.Get.html#Partial"><span class="hs-identifier hs-type">Partial</span></a></span><span> </span><span id="local-6989586621679059163"><span class="annot"><span class="annottext">Maybe ByteString -&gt; Decoder b
</span><a href="#local-6989586621679059163"><span class="hs-identifier hs-var">k</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679059162"><span class="annot"><span class="annottext">Handle
</span><a href="#local-6989586621679059162"><span class="hs-identifier hs-var">h</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-225"></span><span>      </span><span id="local-6989586621679059161"><span class="annot"><span class="annottext">ByteString
</span><a href="#local-6989586621679059161"><span class="hs-identifier hs-var">chunk</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Handle -&gt; Int -&gt; IO ByteString
</span><a href="../../bytestring/src/Data.ByteString.html#hGet"><span class="hs-identifier hs-var">B.hGet</span></a></span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="#local-6989586621679059162"><span class="hs-identifier hs-var">h</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="../../bytestring/src/Data.ByteString.Lazy.Internal.html#defaultChunkSize"><span class="hs-identifier hs-var">L.defaultChunkSize</span></a></span><span>
</span><span id="line-226"></span><span>      </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">ByteString -&gt; Int
</span><a href="../../bytestring/src/Data.ByteString.html#length"><span class="hs-identifier hs-var">B.length</span></a></span><span> </span><span class="annot"><span class="annottext">ByteString
</span><a href="#local-6989586621679059161"><span class="hs-identifier hs-var">chunk</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-227"></span><span>        </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Decoder b -&gt; Handle -&gt; IO (Either (ByteOffset, String) b)
</span><a href="#local-6989586621679059178"><span class="hs-identifier hs-var">feed</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Maybe ByteString -&gt; Decoder b
</span><a href="#local-6989586621679059163"><span class="hs-identifier hs-var">k</span></a></span><span> </span><span class="annot"><span class="annottext">Maybe ByteString
forall a. Maybe a
</span><a href="../../base/src/GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="#local-6989586621679059162"><span class="hs-identifier hs-var">h</span></a></span><span>
</span><span id="line-228"></span><span>        </span><span class="annot"><span class="annottext">Int
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Decoder b -&gt; Handle -&gt; IO (Either (ByteOffset, String) b)
</span><a href="#local-6989586621679059178"><span class="hs-identifier hs-var">feed</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Maybe ByteString -&gt; Decoder b
</span><a href="#local-6989586621679059163"><span class="hs-identifier hs-var">k</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ByteString -&gt; Maybe ByteString
forall a. a -&gt; Maybe a
</span><a href="../../base/src/GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="annot"><span class="annottext">ByteString
</span><a href="#local-6989586621679059161"><span class="hs-identifier hs-var">chunk</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">Handle
</span><a href="#local-6989586621679059162"><span class="hs-identifier hs-var">h</span></a></span><span>
</span><span id="line-229"></span><span>
</span><span id="line-230"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-231"></span><span class="hs-comment">-- $generics</span><span>
</span><span id="line-232"></span><span class="hs-comment">--</span><span>
</span><span id="line-233"></span><span class="hs-comment">-- Beginning with GHC 7.2, it is possible to use binary serialization</span><span>
</span><span id="line-234"></span><span class="hs-comment">-- without writing any instance boilerplate code.</span><span>
</span><span id="line-235"></span><span class="hs-comment">--</span><span>
</span><span id="line-236"></span><span class="hs-comment">-- &gt; {-# LANGUAGE DeriveGeneric #-}</span><span>
</span><span id="line-237"></span><span class="hs-comment">-- &gt;</span><span>
</span><span id="line-238"></span><span class="hs-comment">-- &gt; import Data.Binary</span><span>
</span><span id="line-239"></span><span class="hs-comment">-- &gt; import GHC.Generics (Generic)</span><span>
</span><span id="line-240"></span><span class="hs-comment">-- &gt;</span><span>
</span><span id="line-241"></span><span class="hs-comment">-- &gt; data Foo = Foo</span><span>
</span><span id="line-242"></span><span class="hs-comment">-- &gt;          deriving (Generic)</span><span>
</span><span id="line-243"></span><span class="hs-comment">-- &gt;</span><span>
</span><span id="line-244"></span><span class="hs-comment">-- &gt; -- GHC will automatically fill out the instance</span><span>
</span><span id="line-245"></span><span class="hs-comment">-- &gt; instance Binary Foo</span><span>
</span><span id="line-246"></span><span class="hs-comment">--</span><span>
</span><span id="line-247"></span><span class="hs-comment">-- This mechanism makes use of GHC's efficient built-in generics</span><span>
</span><span id="line-248"></span><span class="hs-comment">-- support.</span><span>
</span><span id="line-249"></span></pre></body></html>